package com.hermes.comics.dialogs;

import android.app.Activity;
import android.app.ProgressDialog;

import android.content.Context;
import android.content.DialogInterface;

import android.widget.BaseAdapter;
import android.widget.Toast;

import com.hermes.comics.R;
import com.hermes.comics.data.Catalog;
import com.hermes.comics.data.Issue;
import com.hermes.comics.data.IssueTag;
import com.hermes.comics.data.Serie;
import com.hermes.comics.data.SerieTag;
import com.hermes.comics.extractors.WebExtractorBuilder;
import com.hermes.comics.extractors.WebExtractor;

import java.net.HttpURLConnection;

import java.util.ArrayList;
import java.util.List;

public class AutoUpdateIssueDialog extends RemoteCatalogDialog implements RemoteCatalogDialog.Interface {
   
  private final List<Issue> issues;
  private final BaseAdapter adapter;

  private Issue currentIssue;
  private Serie serie;
  private String urlForEdit = null;

  public static AutoUpdateIssueDialog build(Activity activity, Serie serie, BaseAdapter adapter) { 
    return new AutoUpdateIssueDialog(activity, serie.issues(), adapter);
  }

  public static AutoUpdateIssueDialog build(Activity activity, Issue issue, BaseAdapter adapter) { 
    List<Issue> issues = new ArrayList<Issue>();
    issues.add(issue);
    return new AutoUpdateIssueDialog(activity, issues, adapter);
  }

  private AutoUpdateIssueDialog(Activity activity, List<Issue> issues, BaseAdapter adapter) {
    super(activity);

    int issues_size = issues.size();
    this.adapter = adapter;
    this.issues = issues;
    this.currentIssue = issues.remove(0);
    this.serie = Catalog.getInstance().getSerie(currentIssue);

    setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    setMax(issues_size * 2);
    setIndeterminate(false);
    setProgressNumberFormat("");
    setTitle(SerieTag.TITLE.value(serie));
    setProgressNumberFormat("Fetching page...");
    setMessage("Issue " + IssueTag.KEY.value(currentIssue));
  }

  @Override
  public void onPreExecute() {
  }

  @Override
  public void onCancelled() {
    dismiss();
  }

  @Override
  public String getUrl() {
    return (urlForEdit == null) ? IssueTag.URL.value(currentIssue) : urlForEdit;
  }

  @Override
  public void onFailure(UrlWrapper result) {
    Toast.makeText(getContext(),
         "Error http status = " + result.httpStatus() + "\nMessage = " + result.message(), Toast.LENGTH_LONG).show();
    dismiss();
  }

  @Override
  public void onSuccess(UrlWrapper url) {
    incrementProgressBy(1);
    if (urlForEdit == null) {
      WebExtractor extractor = WebExtractorBuilder.build(url.url(), url.content());
      if (extractor == null) {
        abort();
        Toast.makeText(getContext(), "There's no extractor for url " + getUrl(), Toast.LENGTH_LONG).show();
        return;
      }

      if (!extractor.extractIssue(currentIssue)) {
        Toast.makeText(getContext(), "Issue could not be extracted. Url = " + getUrl(), Toast.LENGTH_LONG).show();
        dismiss();
        return;
      }

      urlForEdit = currentIssue.urlForEdit();

      setProgressNumberFormat("Saving remotely...");

      execute(this);
    } else {
      Catalog.getInstance().commit(currentIssue);
      adapter.notifyDataSetChanged();

      if (issues.isEmpty()) {
        dismiss();
      } else {
        currentIssue = issues.remove(0);
        urlForEdit = null;
        setMessage("Issue " + IssueTag.KEY.value(currentIssue));
        setProgressNumberFormat("Fetching page...");
        execute(this);
      }
    }
  }

  @Override
  public void show() {
    super.show();
    if (WebExtractorBuilder.canBuild(getUrl())) {
      execute(this);
    } else {
      dismiss();
      Toast.makeText(getContext(), "There's no extractor for url " + getUrl(), Toast.LENGTH_LONG).show();
    }
  }
}
